package com.itheima.day08;

import java.util.HashSet;
import java.util.Objects;
import java.util.Scanner;

public class Practice2 {
    public static void main(String[] args) {
        /**如图所示:当我们在京东注册新用户的时候,我们输入一个要注册的用户名,如果之前有该用户名注册过,
         * 京东是不会让相同的用户名注册的,会注册失败.根据以上描述,设计程序,完成注册功能!*/
        HashSet<User> hashSet = new HashSet<>();//储存User对象的
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请您输入账户名称：");
            String name = sc.next();
            User u = new User(name);
            if (hashSet.add(u)) {
                System.out.println("账户名添加成功！");
            } else {
                System.out.println("账户名已存在，添加失败！");
            }
        }

    }
}
class User{
    private String name ;


    public User() {
    }

    public User(String name  ) {
        this.name = name;

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }





    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return Objects.equals(name, user.name)  ;
    }

    @Override
    public int hashCode() {
        return Objects.hash(name );
    }
}